//
//  JMLoadingButton.m
//  NewiOSProject
//
//  Created by MAC on 2023/9/2.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMLoadingButton.h"
#import <QuartzCore/QuartzCore.h>

@interface JMLoadingButton()

@property (nonatomic,strong)UIView *contentView;
@property (nonatomic,strong)UILabel *titleLable;
@property (nonatomic,strong)UIImageView *loadingImgV;
@property (nonatomic,strong)LoadingButtonAction clickBlock;
@end
@implementation JMLoadingButton

-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)title font:(UIFont *)font titleLabelColor:(UIColor *)color backgoundColor:(nonnull UIColor *)bgColor action:(SEL)action target:(id)target{
    if(self = [super initWithFrame:frame]){
        if (color) {
            self.enableTextColor = color;
        }else{
            self.enableTextColor = Button_ENABLE_TEXTCOLOR();
        }
        if (bgColor) {
            self.enableBgColor = bgColor;
        }else{
            self.enableBgColor = Button_ENABLE_BG_COLOR();
        }
        self.unableTextColor = Button_UNABLE_TEXTCOLOR();
        self.unableableBgColor = Button_UNABLE_BG_COLOR();
        self.backgroundColor = bgColor;
        [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.loadingImgV];
        [self setTitle:title forState:(UIControlStateNormal)];
        [self setTitleColor:color forState:(UIControlStateNormal)];
        self.titleLabel.font = font;
        _loadingImgV.hidden = YES;
    }
    return self;
}

-(void)startLoading{
    [self addSubview:self.loadingImgV];
    [self.loadingImgV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.width.height.equalTo(@13);
    }];
    [self.loadingImgV setHidden:NO];
   

    delayOperation(0, ^{
        CALayer *loadingLayer = [CALayer layer];
        loadingLayer.bounds = CGRectMake(0, 0, self.loadingImgV.width, self.loadingImgV.height);
        loadingLayer.position = CGPointMake(self.loadingImgV.width / 2, self.loadingImgV.height / 2);
        loadingLayer.contents = (id)[UIImage imageNamed:@"button_loading"].CGImage; // 使用你自己的加载图片
        [self.loadingImgV.layer addSublayer:loadingLayer];
        
        CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
        rotationAnimation.duration = 2.0; // 旋转一圈所需的时间
        rotationAnimation.repeatCount = HUGE_VALF; // 无限循环
        [loadingLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
        self.enable = NO;
        delayOperation(0.01, ^{
            if (![self.titleLabel.text isEqualToString:@"Código de verificación de voz >"]) {
                [self setTitleColor:UIColor.clearColor forState:(UIControlStateNormal)];
            }
        });
    });
   
    delayOperation(30, ^{
        [self.loadingImgV setHidden:YES];
        [self.loadingImgV removeFromSuperview];
        [self.loadingImgV.layer removeAllSublayers];
        [self.loadingImgV.layer removeAllAnimations];
    });
}
-(void)setEnable:(BOOL)enable{
    _enable = enable;
    self.userInteractionEnabled = enable;
    self.backgroundColor = enable?self.enableBgColor:self.unableableBgColor;
    [self setTitleColor:enable?self.enableTextColor:self.unableTextColor forState:(UIControlStateNormal)];
}
-(void)endLoading{
    self.enable = YES;
    [self.loadingImgV setHidden:YES];
    [self.loadingImgV removeFromSuperview];
    [self.loadingImgV.layer removeAllSublayers];
    [self.loadingImgV.layer removeAllAnimations];
}

-(UIImageView *)loadingImgV{
    if(!_loadingImgV){
        _loadingImgV = [[UIImageView alloc]initWithFrame:(CGRectMake(0, 0, 13, 13))];
        _loadingImgV.contentMode = UIViewContentModeScaleAspectFill;
        _loadingImgV.centerX = self.width/2;
        _loadingImgV.centerY = self.height/2;
    }
    return _loadingImgV;
}
@end
